#coding=utf-8
#author：zhujingyuan
import logging




class MyLog:

    def my_log(self, msg, level):
        # 定义一个日志收集器my_logger
        my_logger = logging.getLogger('VIPClubAPI')

        # 设定级别,日志等级总开关
        my_logger.setLevel('DEBUG')

        # 设置日志输出格式
        formatter = logging.Formatter('%(asctime)s-%(levelname)s-%(filename)s-%(name)s-log info:%(message)s')

        # 创建一个我们自己的输出渠道
        ch = logging.StreamHandler()
        ch.setLevel('ERROR')
        ch.setFormatter(formatter)

        fh = logging.FileHandler("E:/VIPclub/Log/log.txt", encoding='utf-8')
        fh.setLevel('DEBUG')
        fh.setFormatter(formatter)

        # 两者对接--指定输出渠道
        my_logger.addHandler(ch)
        my_logger.addHandler(fh)

        # 收集日志
        if level == 'DEBUG':
            my_logger.debug(msg)
        elif level == 'INFO':
            my_logger.info(msg)
        elif level == 'WARNING':
            my_logger.warning(msg)
        elif level == 'ERROR':
            my_logger.error(msg)
        elif level == 'CRITICAL':
            my_logger.critical(msg)

        # 关闭渠道
        my_logger.removeHandler(ch)
        my_logger.removeHandler(fh)

    def debug(self, msg):
        self.my_log(msg, 'DEBUG')

    def info(self, msg):
        self.my_log(msg, 'INFO')

    def error(self, msg):
        self.my_log(msg, 'ERROR')

    def critical(self, msg):
        self.my_log(msg, 'CRITICAL')


if __name__ == '__main__':
    my_logger = MyLog()
    my_logger.debug('可不可以这样？？')
    my_logger.info('可行吗？？')
    MyLog().my_log('stone 今天有点萌萌滴1', 'ERROR')
    MyLog().my_log('MIRACLE 今天也有点萌萌滴2', 'ERROR')
    MyLog().my_log('巴啦啦小魔仙 今天也有点萌萌滴3', 'ERROR')
